package controllers

import (
	"github.com/gin-gonic/gin"
	"wakitsoft.com/resume/db"
	"wakitsoft.com/resume/libs"
	"wakitsoft.com/resume/models"
)

func CreateSchool(c *gin.Context) {
	var req models.School
	if libs.ValidateRequest(c, &req) == false {
		return
	}

	db.GetDB().Model(&models.School{}).Create(&req)
	libs.Success(c, &req)
}

func UpdateSchool(c *gin.Context) {
	var req models.School
	if libs.ValidateRequest(c, &req) == false {
		return
	}

	var school models.School
	db.GetDB().Model(&models.School{}).First(&school)
	if req.Name != "" {
		school.Name = req.Name
	}

	if req.Logo != "" {
		school.Logo = req.Logo
	}

	if req.Is985 != nil {
		school.Is985 = req.Is985
	}

	if req.Is211 != nil {
		school.Is211 = req.Is211
	}

	db.GetDB().Save(&school)
	libs.Success(c, &school)
}

func DeleteSchool(c *gin.Context) {
	id := c.Param("id")
	var school models.School
	db.GetDB().Where("id = ?", id).First(&school)
	db.GetDB().Delete(&school)
	libs.Success(c, &school)
}

func GetSchool(c *gin.Context) {
	id := c.Param("id")
	var school models.School
	db.GetDB().Model(models.School{}).Where("id = ?", id).First(&school)
	libs.Success(c, &school)
}

func GetSchools(c *gin.Context) {
	var schools []models.School
	db.GetDB().Model(&models.School{}).Find(&schools)
	libs.Success(c, schools)
}
